import threading
from time import sleep, ctime

#每个线程执行的时间
loop_time_list = (4, 2)

class MyThread(threading.Thread):
    def __init__(self, func, args):
        # 这些参数的含义：1.run中调用的方法，2.方法所用的参数
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
    def run(self):
        self.func(*self.args)

#线程执行的函数
def loop(loop_mem, loop_time):
    print('start loop %s at %s' % (loop_mem, ctime()))
    sleep(loop_time)
    print('loop %s done at %s' % (loop_mem, ctime()))


def main():
    print('Programming start at: %s' % ctime())
    threads = []
    #线程数
    loop_num = range(len(loop_time_list))

    #create threads
    for i in loop_num:
        t = MyThread(loop, (i, loop_time_list[i]))
        threads.append(t)

    #start threads
    for i in loop_num:
        threads[i].start()

    print('all done at: %s' % ctime())

if __name__ == '__main__':
    main()